package fr.insee.sicape;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import fr.insee.sicape.bean.Apet;
import fr.insee.sicape.bean.Echo;
import fr.insee.sicape.bean.Reponse;
import fr.insee.sicape.format.Formater;
import fr.insee.sicape.format.FormaterFactory;
import fr.insee.sicape.service.ReponseService;

public class Sicape {

	public static String
		CCM = "CCM",
		CCS = "CCS",
		NOMEN = "NOMEN"
	;
	
	public static void main(String[] args) throws IOException {
		
		System.out.println("Running...");
		
		ResourceBundle bundle = ResourceBundle.getBundle("sicape");
		String in = bundle.getString("sicape.in");
		System.out.println("Fichier en entree : " + in);
		String out = bundle.getString("sicape.out");
		System.out.println("Fichier en sortie : " + out);
		int index = Integer.parseInt(bundle.getString("sicape.index"));
		int seuil = Integer.parseInt(bundle.getString("sicape.seuil"));
		System.out.println("Seuil de validite des echos : " + seuil);
		int maxDigits = Integer.parseInt(bundle.getString("sicape.digits.max"));
		int minDigits = Integer.parseInt(bundle.getString("sicape.digits.min"));
		Pattern
			patternRetour = Pattern.compile(bundle.getString("sicape.pattern.retour")),
			patternEcho = Pattern.compile(bundle.getString("sicape.pattern.echo")),
			patternApet = Pattern.compile(bundle.getString("sicape.pattern.apet")),
			patternFreq = Pattern.compile(bundle.getString("sicape.pattern.freq"))
		;
		ReponseService service = ReponseService.getInstance();
		Formater formater = FormaterFactory.getFormater();
		

		InputStream stream = new FileInputStream(in);
		Reader reader = new InputStreamReader(stream);
		BufferedReader buffer = new BufferedReader(reader);
		Writer writer = new FileWriter(out);
		String line = null;
		while((line = buffer.readLine()) != null){
			String info = line.substring(0, index - 1);
			String chaineReponse = line.substring(index -1, line.length());
			Matcher matcherRetour = patternRetour.matcher(chaineReponse);
			if(matcherRetour.find()){
				
				Reponse reponse = new Reponse();
				String retour = matcherRetour.group(1).trim();
				reponse.setCodeRetour(retour);
				reponse.setInfoPassives(info);
				
				if(CCS.equals(retour) || CCM.equals(retour)){
					
					Matcher matcherEcho = patternEcho.matcher(chaineReponse);
					while(matcherEcho.find()){
						
						Echo echo = new Echo();
						String chaineEcho = matcherEcho.group(1);
						Matcher matcherApet = patternApet.matcher(chaineEcho);
						Matcher matcherFreq = patternFreq.matcher(chaineEcho);
						if(matcherFreq.find()){
							int freq = 0, freq5 = 0;
							if(NOMEN.equals(matcherFreq.group(0).trim())){
								freq = 1;
								freq5 = 1;
							}
							else{
								freq = Integer.parseInt(matcherFreq.group(1).trim());
								freq5 = Integer.parseInt(matcherFreq.group(2).trim());
							}
							echo.setFrequence(freq);
							echo.setFrequence5(freq5);
						}
						while(matcherApet.find()){
							String code =  matcherApet.group(1);
							int pourcentage =  Integer.parseInt(matcherApet.group(2).trim());
							Apet apet = new Apet(code, pourcentage);
							echo.add(apet);
						}
						reponse.add(echo);
					}
					String code = service.getMeilleurCode(reponse, seuil, minDigits, maxDigits);
					writer.append(formater.format(reponse, code, chaineReponse));
				}
				else{
					writer.append(formater.format(reponse, null, chaineReponse));
				}
			}
		}
		writer.flush();
		writer.close();
		buffer.close();
		reader.close();
		stream.close();
		
		System.out.println("Done.");
		System.exit(0);
	}

}
